package hanlder

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"role-guarderrerr/cache"
	"role-guarderrerr/models"
	"role-guarderrerr/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "login_" + username
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "错误三次，禁止登陆",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username = ?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "禁止登陆两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "登陆成功",
		"data": user,
	})
	return
}
func List(c *gin.Context) {
	id := c.Query("id")
	var user models.ArticleOrderList
	redis_key := "Article_List" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_name as OrderName",
				"orders.order_to as OrderTo",
				"articles.article_name as ArticleName",
				"articles.article_content as ArticleContent",
				"articles.article_author as ArticleAuthor",
			).
			Joins("LEFT JOIN articles ON articles.id = orders.aid").
			Where("id = ?", id).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "ok",
		"data": user,
	})
	return
}
func Add(c *gin.Context) {
	per_name := c.PostForm("per_name")
	per_price, _ := strconv.Atoi(c.PostForm("per_price"))
	if per_name == "" || per_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Per{
		PerName:  per_name,
		PerPrice: per_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PerGitList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var user []models.PerGitList
	redis_key := "per_git_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("gits").
			Select(
				"gits.id as GitId",
				"gits.git_name as GitName",
				"gits.git_title as GitTitle",
				"pers.per_name as PerName",
				"pers.per_price as PerPrice",
			).
			Joins("LEFT JOIN pers ON pers.id = gits.pid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}

	var total int64
	models.DB.Model(models.PerGitList{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  user,
		"total": total,
	})
	return
}
func PerGitList1(c *gin.Context) {
	pid := c.Query("pid")
	var user models.PerGitList
	redis_key := "per_list" + pid
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("gits").
			Select(
				"gits.id as GitId",
				"gits.git_name as GitName",
				"gits.git_title as GitTitle",
				"pers.per_name as PerName",
				"pers.per_price as PerPrice",
			).
			Joins("LEFT JOIN pers ON pers.id = gits.pid").
			Where("gits.pid = ?", pid).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": user,
	})
	return
}
